package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.QcDataMapper;
import com.zhijian.medical.entity.po.QcData;
import com.zhijian.medical.entity.vo.request.QcDataSearchVo;
import com.zhijian.medical.entity.vo.response.QcDataVo;
import com.zhijian.medical.service.QcDataService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 质控数据业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-10-19 13:47:47
 */
@Slf4j
@Service
public class QcDataServiceImpl implements QcDataService {

    @Autowired
    private QcDataMapper mapper;

    @Override
    public QcData selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public QcDataVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(QcData qcData) {
        Date now = new Date();
        qcData.setCreateTime(now);
        qcData.setUpdateTime(now);
        return mapper.insert(qcData) > 0 ? qcData.getId() : 0L;
    }

    @Override
    public boolean update(QcData qcData) {
        Date now = new Date();
        qcData.setUpdateTime(now);
        return mapper.updateByPrimaryKey(qcData) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(QcData record) {
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(QcDataSearchVo qcDataSearchVo) {
        return mapper.count(qcDataSearchVo);
    }

    @Override
    public List<QcDataVo> selectList(QcDataSearchVo qcDataSearchVo) {
        return mapper.selectList(qcDataSearchVo);
    }

    @Override
    public Page<QcDataVo> selectPage(QcDataSearchVo qcDataSearchVo) {
        //先直接查总数
        int count = count(qcDataSearchVo);
        //分页
        Page<QcDataVo> page = new Page<>(true, count, qcDataSearchVo.getPageNum(), qcDataSearchVo.getPageSize());
        qcDataSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<QcDataVo> responseVos = this.selectList(qcDataSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean updateValueAndStatusByQcIdAndDate(String value, Byte status, Long qcId, String date) {
        return mapper.updateValueAndStatusByQcIdAndDate(value, status, qcId, date) != 0;
    }

}